﻿// dlg_img_ctrl.cpp: 实现文件
//

#include "pch.h"
#include "CamCtrl.h"
#include "afxdialogex.h"
#include "dlg_img_ctrl.h"
#include "spk_osd.h"
//#include "spk_bmp.h"
//#include <opencv2/opencv.hpp>

// dlg_img_ctrl 对话框

IMPLEMENT_DYNAMIC(dlg_img_ctrl, CDialogEx)

dlg_img_ctrl::dlg_img_ctrl(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_IMG_CTRL, pParent)
{

}

dlg_img_ctrl::~dlg_img_ctrl()
{
}

void dlg_img_ctrl::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(dlg_img_ctrl, CDialogEx)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void dlg_img_ctrl::OnDraw(CDC* pDC)
{
	CRect rect;
	GetDlgItem(IDC_STATIC_IMAGE_PREVIEW)->GetWindowRect(&rect);
	ScreenToClient(&rect);

	CImage image;
	image.Create(rect.Width(), rect.Height(), 24);// 这里生成的24可能不是你想的24

	int rows = image.GetHeight();
	int cols = image.GetWidth();

	/*图像的间距。 如果返回值为负数，则位图为自下而上的 DIB，其原点为左下角。 如果返回值为正数，则位图为自上而下的 DIB，其原点为左上角*/

	int destPitch = image.GetPitch();//这里还真是负的

	// 这里有一个坑, 就是这里的位图是会做字节对齐的, 如果你的长或者宽有一个不是4的倍数, 那么你之后取处理就不能按3来处理

	/*指向位图缓冲区的指针。 如果位图是自下而上的 DIB，则指针指向缓冲区末端附近。 如果位图是自上而下的 DIB，则指针指向缓冲区的第一个字节。*/
	BYTE* data = (BYTE*)image.GetBits();
	BYTE* p = data;
	spk_osd osd;

	BYTE* q = data;

	for (int i = 0; i < rows; i++)
	{
		int pos = 0;
		for (int j = 0; j < cols; j++)
		{
			int pixel[3] = { j,j,j }; /*水平渐变图像*/
			p[pos] = pixel[0] & 0xFF;
			p[pos + 1] = pixel[1] & 0xFF;
			p[pos + 2] = pixel[2] & 0xFF;
			pos += 3;
		}
		p += destPitch;
	}

	p -= destPitch;

	for (int i = 0; i < cols * 3 / 2; i += 3)
	{
		q[i] = 0;
		q[i + 1] = 0;
		q[i + 2] = 255;
	}

	for (int i = 0; i < cols * 3 / 2; i += 3)
	{
		p[i] = 255;
		p[i + 1] = 0;
		p[i + 2] = 0;
	}

	device_info info;
	memset(&info, 0, sizeof(device_info));//windows平台上面自动初始化不会清零
	get(&info);

	osd.set_img_ptr(data, cols, rows);

	osd.add_((char*)info.osd_info[0], info.osd_info_location[0][0], info.osd_info_location[0][1], OSD_SIZE_48, RGB(0, 0, 0));
	osd.add_((char*)info.osd_info[1], info.osd_info_location[1][0], info.osd_info_location[1][1], OSD_SIZE_32, RGB(255, 255, 255));
	osd.add_((char*)info.osd_info[2], info.osd_info_location[2][0], info.osd_info_location[2][1], OSD_SIZE_24, RGB(0, 0, 255));
	osd.add_((char*)info.osd_info[3], info.osd_info_location[3][0], info.osd_info_location[3][1], OSD_SIZE_16, RGB(0, 255, 0));


	image.Draw(pDC->m_hDC, rect);
}

void dlg_img_ctrl::OnPaint()
{
	CPaintDC dc(this);
	OnDraw(&dc);
}



int dlg_img_ctrl::set(struct device_info* info)
{
	CString str;
	str.Format("%d", info->video_iw);
	GetDlgItem(IDC_EDIT_VIDEO_IW)->SetWindowText(str);
	str.Format("%d", info->video_ih);
	GetDlgItem(IDC_EDIT_VIDEO_IH)->SetWindowText(str);
	str.Format("%d", info->video_encode_bps);
	GetDlgItem(IDC_EDIT_VIDEO_BPS)->SetWindowText(str);
	str.Format("%d", info->video_frame_rate);
	GetDlgItem(IDC_EDIT_VIDEO_FRAME_RATE)->SetWindowText(str);
	((CButton*)GetDlgItem(IDC_EN_OSD_CHANNEL_NAME))->SetCheck(info->osd_en_channel_name ? TRUE : FALSE);
	((CButton*)GetDlgItem(IDC_EN_OSD_DATE))->SetCheck(info->osd_en_date_info ? TRUE : FALSE);
	((CButton*)GetDlgItem(IDC_EN_OSD_WEEK))->SetCheck(info->osd_en_week_info ? TRUE : FALSE);
	GetDlgItem(IDC_EDIT_OSD_CHANNEL_NAME)->SetWindowText(CString(info->osd_channel_name));

	((CComboBox*)GetDlgItem(IDC_OSD_TIME_FORMAT))->SetCurSel(info->osd_time_format);
	((CComboBox*)GetDlgItem(IDC_OSD_DATE_FORMAT))->SetCurSel(info->osd_date_format);

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO1))->SetCheck(CString(info->osd_info[0]) == "" ? FALSE : TRUE);
	GetDlgItem(IDC_EDIT_OSD_INFO1)->SetWindowText(CString(info->osd_info[0]));

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO2))->SetCheck(CString(info->osd_info[1]) == "" ? FALSE : TRUE);
	GetDlgItem(IDC_EDIT_OSD_INFO2)->SetWindowText(CString(info->osd_info[1]));

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO3))->SetCheck(CString(info->osd_info[2]) == "" ? FALSE : TRUE);
	GetDlgItem(IDC_EDIT_OSD_INFO3)->SetWindowText(CString(info->osd_info[2]));

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO4))->SetCheck(CString(info->osd_info[3]) == "" ? FALSE : TRUE);
	GetDlgItem(IDC_EDIT_OSD_INFO4)->SetWindowText(CString(info->osd_info[3]));

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO5))->SetCheck(CString(info->osd_info[4]) == "" ? FALSE : TRUE);
	//GetDlgItem(IDC_EDIT_OSD_INFO5)->SetWindowText(CString(info->osd_info[4]));

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO6))->SetCheck(CString(info->osd_info[5]) == "" ? FALSE : TRUE);
	//GetDlgItem(IDC_EDIT_OSD_INFO6)->SetWindowText(CString(info->osd_info[5]));

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO7))->SetCheck(CString(info->osd_info[6]) == "" ? FALSE : TRUE);
	//GetDlgItem(IDC_EDIT_OSD_INFO7_)->SetWindowText(CString(info->osd_info[6]));

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO8))->SetCheck(CString(info->osd_info[7]) == "" ? FALSE : TRUE);
	//GetDlgItem(IDC_EDIT_OSD_INFO8)->SetWindowText(CString(info->osd_info[7]));

	str.Format("%d", info->osd_info_location[0][0]);
	GetDlgItem(IDC_EDIT_OSD1_X)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[0][1]);
	GetDlgItem(IDC_EDIT_OSD1_Y)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[1][0]);
	GetDlgItem(IDC_EDIT_OSD2_X)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[1][1]);
	GetDlgItem(IDC_EDIT_OSD2_Y)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[2][0]);
	GetDlgItem(IDC_EDIT_OSD3_X)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[2][1]);
	GetDlgItem(IDC_EDIT_OSD3_Y)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[3][0]);
	GetDlgItem(IDC_EDIT_OSD4_X)->SetWindowText(str);
	str.Format("%d", info->osd_info_location[3][1]);
	GetDlgItem(IDC_EDIT_OSD4_Y)->SetWindowText(str);

	return 0;
}

int dlg_img_ctrl::get(struct device_info* info)
{
	CString str;
	GetDlgItem(IDC_EDIT_VIDEO_IW)->GetWindowText(str);         info->video_iw = atoi(str);
	GetDlgItem(IDC_EDIT_VIDEO_IH)->GetWindowText(str);         info->video_ih = atoi(str);
	GetDlgItem(IDC_EDIT_VIDEO_BPS)->GetWindowText(str);        info->video_encode_bps = atoi(str);
	GetDlgItem(IDC_EDIT_VIDEO_FRAME_RATE)->GetWindowText(str); info->video_frame_rate = atoi(str);

	info->osd_en_channel_name = ((CButton*)GetDlgItem(IDC_EN_OSD_CHANNEL_NAME))->GetCheck() == TRUE ? 1 : 0;
	info->osd_en_date_info = ((CButton*)GetDlgItem(IDC_EN_OSD_DATE))->GetCheck() == TRUE ? 1 : 0;
	info->osd_en_week_info = ((CButton*)GetDlgItem(IDC_EN_OSD_WEEK))->GetCheck() == TRUE ? 1 : 0;

	GetDlgItem(IDC_EDIT_OSD_CHANNEL_NAME)->GetWindowText(str); memcpy(info->osd_channel_name, str.GetBuffer(), str.GetLength());

	info->osd_time_format = ((CComboBox*)GetDlgItem(IDC_OSD_TIME_FORMAT))->GetCurSel();
	info->osd_date_format = ((CComboBox*)GetDlgItem(IDC_OSD_DATE_FORMAT))->GetCurSel();

	GetDlgItem(IDC_EDIT_OSD_INFO1)->GetWindowText(str);  memcpy(info->osd_info[0], str.GetBuffer(), str.GetLength());
	GetDlgItem(IDC_EDIT_OSD_INFO2)->GetWindowText(str);  memcpy(info->osd_info[1], str.GetBuffer(), str.GetLength());
	GetDlgItem(IDC_EDIT_OSD_INFO3)->GetWindowText(str);  memcpy(info->osd_info[2], str.GetBuffer(), str.GetLength());
	GetDlgItem(IDC_EDIT_OSD_INFO4)->GetWindowText(str);  memcpy(info->osd_info[3], str.GetBuffer(), str.GetLength());
	//GetDlgItem(IDC_EDIT_OSD_INFO5)->GetWindowText(str);  memcpy(info->osd_info[4], str.GetBuffer(), str.GetLength());
	//GetDlgItem(IDC_EDIT_OSD_INFO6)->GetWindowText(str);  memcpy(info->osd_info[5], str.GetBuffer(), str.GetLength());
	//GetDlgItem(IDC_EDIT_OSD_INFO7_)->GetWindowText(str); memcpy(info->osd_info[6], str.GetBuffer(), str.GetLength());
	//GetDlgItem(IDC_EDIT_OSD_INFO8)->GetWindowText(str);  memcpy(info->osd_info[7], str.GetBuffer(), str.GetLength());
	GetDlgItem(IDC_EDIT_OSD1_X)->GetWindowText(str);         info->osd_info_location[0][0] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD1_Y)->GetWindowText(str);         info->osd_info_location[0][1] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD2_X)->GetWindowText(str);         info->osd_info_location[1][0] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD2_Y)->GetWindowText(str);         info->osd_info_location[1][1] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD3_X)->GetWindowText(str);         info->osd_info_location[2][0] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD3_Y)->GetWindowText(str);         info->osd_info_location[2][1] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD4_X)->GetWindowText(str);         info->osd_info_location[3][0] = atoi(str);
	GetDlgItem(IDC_EDIT_OSD4_Y)->GetWindowText(str);         info->osd_info_location[3][1] = atoi(str);

	return 0;
}

BOOL dlg_img_ctrl::valid(void)
{
	return TRUE;
}

BOOL dlg_img_ctrl::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	Invalidate(FALSE);

	GetDlgItem(IDC_EDIT_VIDEO_IW)->SetWindowText("");
	GetDlgItem(IDC_EDIT_VIDEO_IH)->SetWindowText("");
	GetDlgItem(IDC_EDIT_VIDEO_BPS)->SetWindowText("");
	GetDlgItem(IDC_EDIT_VIDEO_FRAME_RATE)->SetWindowText("");
	((CButton*)GetDlgItem(IDC_EN_OSD_CHANNEL_NAME))->SetCheck(FALSE);
	((CButton*)GetDlgItem(IDC_EN_OSD_DATE))->SetCheck(FALSE);
	((CButton*)GetDlgItem(IDC_EN_OSD_WEEK))->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_OSD_CHANNEL_NAME)->SetWindowText("");

	((CComboBox*)GetDlgItem(IDC_OSD_TIME_FORMAT))->InsertString(0, "12小时制");
	((CComboBox*)GetDlgItem(IDC_OSD_TIME_FORMAT))->InsertString(1, "24小时制");
	((CComboBox*)GetDlgItem(IDC_OSD_TIME_FORMAT))->SetCurSel(0);

	((CComboBox*)GetDlgItem(IDC_OSD_DATE_FORMAT))->InsertString(0, "YYMMDD");
	((CComboBox*)GetDlgItem(IDC_OSD_DATE_FORMAT))->InsertString(1, "YY-MM-DD");
	((CComboBox*)GetDlgItem(IDC_OSD_DATE_FORMAT))->SetCurSel(0);

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO1))->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_OSD_INFO1)->SetWindowText("");

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO2))->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_OSD_INFO2)->SetWindowText("");

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO3))->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_OSD_INFO3)->SetWindowText("");

	((CButton*)GetDlgItem(IDC_EN_OSD_INFO4))->SetCheck(FALSE);
	GetDlgItem(IDC_EDIT_OSD_INFO4)->SetWindowText("");

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO5))->SetCheck(FALSE);
	//GetDlgItem(IDC_EDIT_OSD_INFO5)->SetWindowText("");

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO6))->SetCheck(FALSE);
	//GetDlgItem(IDC_EDIT_OSD_INFO6)->SetWindowText("");

	//((CButton*)GetDlgItem(IDC_EDIT_OSD_INFO7_))->SetCheck(FALSE);
	//GetDlgItem(IDC_EDIT_OSD_INFO7_)->SetWindowText("");

	//((CButton*)GetDlgItem(IDC_EN_OSD_INFO8))->SetCheck(FALSE);
	//GetDlgItem(IDC_EDIT_OSD_INFO8)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD1_X)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD1_Y)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD2_X)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD2_Y)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD3_X)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD3_Y)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD4_X)->SetWindowText("");
	GetDlgItem(IDC_EDIT_OSD4_Y)->SetWindowText("");

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
